package com.icez.base_compose_framework.di

import android.content.Context
import coil.ImageLoader
import com.icez.base_compose_framework.dispatchers.AppDispatchers
import com.icez.base_compose_framework.dispatchers.Dispatcher
import com.rometools.rome.io.SyndFeedInput
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.LoggingEventListener
import java.io.File
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object DataDiModule {

    @Provides
    @Singleton
    fun provideOkHttpClient(@ApplicationContext context:Context):OkHttpClient = OkHttpClient.Builder()
        .cache(Cache(File(context.cacheDir,"http_cache"),(20*1024*1024).toLong()))
        .apply {
//            if(BuildConfig.DEBUG){
//            }
            eventListenerFactory(LoggingEventListener.Factory())

        }
        .build()



    @Provides
    @Singleton
    fun provideImageLoader(@ApplicationContext context:Context):ImageLoader = ImageLoader.Builder(context)
        .respectCacheHeaders(false)
        .build()



    @Provides
    @Singleton
    fun provideSyndFeedInput() = SyndFeedInput()

    @Provides
    @Dispatcher(AppDispatchers.IO)
    @Singleton
    fun provideIODispacher(): CoroutineDispatcher = Dispatchers.IO

    @Provides
    @Dispatcher(AppDispatchers.Main)
    @Singleton
    fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
}